package defpackage; import com.baidu.tts.loopj.AsyncHttpResponseHandler; import com.facebook.ads.AudienceNetworkActivity; import com.google.firebase.perf.FirebasePerformance; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.HashMap; import org.apache.james.mime4j.util.CharsetUtil; import org.json.JSONException; /* access modifiers changed from: package-private */ /* renamed from: fwp reason: default package */ public final class fwp { /* JADX WARNING: Removed duplicated region for block: B:18:0x0076 */ public static fwl T(String str, String str2, String str3) throws IOException, JSONException { Throwable th; HttpURLConnection httpURLConnection = null; r1 = null; fwl fwl = null; HashMap hashMap = new HashMap(); if (str2 != null) { hashMap.put("password", str2); } hashMap.put("suffix", str3); hashMap.put("wps_sid", fok.bBq().getWPSSid()); hashMap.put("secretKey", "47h05014fe85drt89gr7b86ea8eg713e547he9f3"); try { HttpURLConnection httpURLConnection2 = (HttpURLConnection) new URL("https://odf2wps.wps.com/oocsvr/document/convert").openConnection(); try { httpURLConnection2.setDoInput(true); httpURLConnection2.setDoOutput(true); httpURLConnection2.setUseCaches(false); httpURLConnection2.setRequestMethod(FirebasePerformance.HttpMethod.POST); httpURLConnection2.setRequestProperty("Charset", AsyncHttpResponseHandler.DEFAULT_CHARSET); httpURLConnection2.setRequestProperty("Connection", "Keep-Alive"); httpURLConnection2.setRequestProperty("Content-Type", "multipart/form-data;boundary=----------ThIs_Is_tHe_bouNdaRY_$"); a(httpURLConnection2, hashMap, str); String d = d(httpURLConnection2); if (d != null && !d.isEmpty()) { fwl = new fwl(d); } if (httpURLConnection2 != null) { httpURLConnection2.disconnect(); } return fwl; } catch (Throwable th2) { httpURLConnection = httpURLConnection2; th = th2; if (httpURLConnection != null) { } throw th; } } catch (Throwable th3) { th = th3; if (httpURLConnection != null) { httpURLConnection.disconnect(); } throw th; } } private static void a(DataOutputStream dataOutputStream, String str) throws IOException { FileInputStream fileInputStream; try { fileInputStream = new FileInputStream(str); try { byte[] bArr = new byte[4096]; while (true) { int read = fileInputStream.read(bArr); if (read != -1) { dataOutputStream.write(bArr, 0, read); } else { x(fileInputStream); return; } } } catch (Throwable th) { th = th; x(fileInputStream); throw th; } } catch (Throwable th2) { th = th2; fileInputStream = null; x(fileInputStream); throw th; } } private static void a(OutputStream outputStream) { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } private static void a(HttpURLConnection httpURLConnection, HashMap<String, String> hashMap, String str) throws IOException { DataOutputStream dataOutputStream; try { dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream()); try { if (!hashMap.isEmpty()) { for (String str2 : hashMap.keySet()) { dataOutputStream.writeBytes("------------ThIs_Is_tHe_bouNdaRY_$\r\n"); dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"" + str2 + "\"\r\n"); dataOutputStream.writeBytes(CharsetUtil.CRLF); dataOutputStream.writeBytes(hashMap.get(str2) + CharsetUtil.CRLF); } } dataOutputStream.writeBytes("------------ThIs_Is_tHe_bouNdaRY_$\r\n"); dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"file\"\r\n"); dataOutputStream.writeBytes(CharsetUtil.CRLF); a(dataOutputStream, str); dataOutputStream.writeBytes(CharsetUtil.CRLF); dataOutputStream.writeBytes("------------ThIs_Is_tHe_bouNdaRY_$--\r\n"); dataOutputStream.flush(); a(dataOutputStream); } catch (Throwable th) { th = th; a(dataOutputStream); throw th; } } catch (Throwable th2) { th = th2; dataOutputStream = null; a(dataOutputStream); throw th; } } public static boolean bT(String str, String str2) throws IOException { FileOutputStream fileOutputStream; InputStream inputStream = null; try { URLConnection openConnection = new URL(str).openConnection(); openConnection.setRequestProperty("wps_sid", fok.bBq().getWPSSid()); InputStream inputStream2 = openConnection.getInputStream(); try { fileOutputStream = new FileOutputStream(str2); } catch (Throwable th) { th = th; fileOutputStream = null; inputStream = inputStream2; x(inputStream); a(fileOutputStream); throw th; } try { byte[] bArr = new byte[1024]; while (true) { int read = inputStream2.read(bArr); if (read > 0) { fileOutputStream.write(bArr, 0, read); } else { x(inputStream2); a(fileOutputStream); return true; } } } catch (Throwable th2) { th = th2; inputStream = inputStream2; x(inputStream); a(fileOutputStream); throw th; } } catch (Throwable th3) { th = th3; fileOutputStream = null; x(inputStream); a(fileOutputStream); throw th; } } private static String d(HttpURLConnection httpURLConnection) throws IOException { String str; int i = 0; InputStream inputStream = null; try { inputStream = httpURLConnection.getInputStream(); int available = inputStream.available(); if (available <= 0 || available > 2048) { StringBuilder sb = new StringBuilder(); byte[] bArr = new byte[2048]; while (true) { int read = inputStream.read(bArr); if (read <= 0) { break; } sb.append(new String(bArr, 0, read, AudienceNetworkActivity.WEBVIEW_ENCODING)); } str = sb.toString(); } else { byte[] bArr2 = new byte[available]; while (inputStream.available() > 0 && i < available) { i += inputStream.read(bArr2, i, available - i); } str = new String(bArr2, AudienceNetworkActivity.WEBVIEW_ENCODING); } return str; } finally { x(inputStream); } } private static void x(InputStream inputStream) { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }